package com.yunkaweilai.android.activity.member;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.lazylibrary.util.StringUtils;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.model.member.MemberHeadModel;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.TitleBuilder;
import com.yunkaweilai.android.utils.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 修改支付密码
 * Created by Administrator on 2017/7/4.
 */

public class UpdatePayPasswordActivity extends BaseActivity {

    public static final String MEMBER_UPDATE_PASS_MODEL = "MEMBER_UPDATE_PASS_MODEL";
    public static final String MEMBER_UPDATE_PASS_BUNDLE = "MEMBER_UPDATE_PASS_BUNDLE";
    @BindView(R.id.id_img_head)
    ImageView idImgHead;
    @BindView(R.id.id_tv_name)
    TextView idTvName;
    @BindView(R.id.id_tvcard_number)
    TextView idTvcardNumber;
    @BindView(R.id.id_tv_card_infor)
    TextView idTvCardInfor;
    @BindView(R.id.id_edt_new_pass)
    EditText idEdtNewPass;
    @BindView(R.id.id_edt_new_confirm_pass)
    EditText idEdtNewConfirmPass;
    @BindView(R.id.titlebar_iv_left)
    ImageView titlebarIvLeft;
    @BindView(R.id.titlebar_tv_left)
    TextView titlebarTvLeft;
    @BindView(R.id.titlebar_tv)
    TextView titlebarTv;
    @BindView(R.id.titlebar_iv_right_2)
    ImageView titlebarIvRight2;
    @BindView(R.id.titlebar_iv_right)
    ImageView titlebarIvRight;
    @BindView(R.id.titlebar_tv_right)
    TextView titlebarTvRight;
    @BindView(R.id.id_view_underline)
    View idViewUnderline;
    @BindView(R.id.rl_titlebar)
    RelativeLayout rlTitlebar;
    @BindView(R.id.linearLayout)
    LinearLayout linearLayout;
    @BindView(R.id.id_check_pass)
    CheckBox idCheckPass;
    @BindView(R.id.id_check_pass2)
    CheckBox idCheckPass2;

    private MemberHeadModel model;

    public static void actionStart(Context context, MemberHeadModel model) {
        Intent intent = new Intent(context, UpdatePayPasswordActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(MEMBER_UPDATE_PASS_MODEL, model);
        intent.putExtra(MEMBER_UPDATE_PASS_BUNDLE, bundle);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_pay_password);
        ButterKnife.bind(this);
        model = (MemberHeadModel) getIntent().getBundleExtra(MEMBER_UPDATE_PASS_BUNDLE).getSerializable(MEMBER_UPDATE_PASS_MODEL);
        initHeadView();
        initClick();
    }

    private void initClick() {
        idCheckPass.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if (isChecked) {
                    //如果选中，显示密码
                    idEdtNewPass.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    //否则隐藏密码
                    idEdtNewPass.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                idEdtNewPass.setSelection(idEdtNewPass.length());
            }
        });
        idCheckPass2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if (isChecked) {
                    //如果选中，显示密码
                    idEdtNewConfirmPass.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    //否则隐藏密码
                    idEdtNewConfirmPass.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                idEdtNewConfirmPass.setSelection(idEdtNewConfirmPass.length());
            }
        });

    }

    //头部信息
    private void initHeadView() {
        new TitleBuilder(mAc).setTitleText("修改支付密码").setLeftImage(R.mipmap.ic_go_back)
                .setRightText("保存").setLeftOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        }).setRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!StringUtils.isEmpty(idEdtNewPass.getText().toString()) || !StringUtils.isEmpty(idEdtNewConfirmPass.getText().toString())) {
                    if (idEdtNewPass.getText().toString().equals(idEdtNewConfirmPass.getText().toString())) {
                        showProgressDialog("请稍等，正在修改密码...");
                        initHttp();
                    } else {
                        showToast("两次密码输入不一致");
                    }
                } else {
                    showToast("还未输入密码");
                }
            }
        });
        Utils.MemberHeadImg(mAc, model.getMember_avatar(), model.getMember_sex(), idImgHead);
        idTvName.setText(model.getMember_name() + " (" + model.getLevel_name() + ": " + model.getCard_no() + ")");
        idTvCardInfor.setText("折扣：" + model.getDiscount() / 10 + "折   积分：" + model.getMember_points() + "   余额：" + model.getMember_price());
    }

    private void initHttp() {
        HttpRequest.postUrl(Api.UPDATE_PAY_PASSWORD)
                .addParams("change_paypassword", idEdtNewPass.getText().toString())
                .addParams("change_paypasswords", idEdtNewConfirmPass.getText().toString())
                .addParams("id", model.getId())
                .addParams("is_password","1")
                .addParams("member_from", "MemberFromAndroid")
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(mAc, json);
                        dismissProgressDialog();
                        if (checkCode) {
                            showToast("修改成功");
                            finish();
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        showToast("系统异常");
                        dismissProgressDialog();
                    }
                });
    }
}
